<?php
/**
 *
 * Application for the function: Update Maintenance
 *
 * This page allows a technician to update maintenance information.
 *
 *
 * @author Chi Zhang, Steven James
 * @version 1.0
 * @package dummy-auto
 *
 */

include('../config.inc');
require('dummy-app.inc');

function emp_type($empid) {
        $sql = mysql_query("
                select  Type
                  from  Employee
                 where  EmpID = $empid;
               ");

        if ($sql) {
                $row = mysql_fetch_array($sql);
                return $row[0];
        } else
                exit_404(mysql_error());
}

session_start();

if ( ! ( session_id() && isset($_SESSION['empid'])
         && emp_type($_SESSION['empid']) == 'T')   )
{
        $tree = make_app("Update Maintenance :: Invalid User!");
        $app = $tree->app;
        $app->addChild('p', "Only Authorized Technician (Employee) Can Use This Function.");

        if (! ( session_id() && isset($_SESSION['empid'])) )
                session_end();

} elseif ($_GET['update']) {

        if (! ( $_GET['vin'] && $_GET['date'] && $_GET['timeS'] &&
                $_GET['timeE'] && isset($_GET['work']) && isset($_GET['cost'])
                    ) )
                exit_404('All field need to be entered');

        $get_wn = "
                select  WorkNum
                  from  Maintain
                 where  VIN = '$_GET[vin]'
                   and  Date = '$_GET[date]'
                   and  TimeS = '$_GET[timeS]';
        ";

        if (! ($sql_list_1 = mysql_query($get_wn)) ) exit_404(mysql_error());

        if (mysql_num_rows($sql_list_1) == 0) {

                if (! mysql_query("
                        insert into Maintain value (
                        0, $_SESSION[empid], '$_GET[vin]',
                        '$_GET[date]', '$_GET[timeS]', '$_GET[timeE]',
                        '$_GET[work]', $_GET[cost]);
                            ") ) exit_404(mysql_error());               

        } elseif (mysql_num_rows($sql_list_1) == 1) {

                $row = mysql_fetch_array($sql_list_1);

                if (! mysql_query("
                        update  Maintain
                           set  EmpID = $_SESSION[empid],
                                TimeE = '$_GET[timeE]',
                                WorkPerformed = '$_GET[work]',
                                Cost = '$_GET[cost]'
                         where  WorkNum = $row[0];
                            ") ) exit_404(mysql_error());

               if (! mysql_query("commit;")) exit_404(mysql_error());

        } else exit_404('Error: Duplicate Schedule Information!');


        $tree = make_app("Update Maintenance :: Update Successful");
        $app = $tree->app;
        $app->addChild('h2', "Maintenance have been updated successfully!");

        $app->addChild('input');
        $app->input['type'] = 'button';
        $app->input['value'] = 'Finish';
        $app->input['onclick'] = "load_app('technician')";

} else {

        $tree = make_app("Update Maintenance");
        $app = $tree->app;
        $app->addChild('h2', "Update a Maintenance Record:");

        $app->addChild('form');
        $app->form['action'] = 'app/technician.php';
        $app->form->addChild('p', "Please enter the following information:");

        $app->form->addChild('table')->addChild('tr');
        $app->form->table->tr->addChild('td', "Car VIN: ");
        $app->form->table->tr->addChild('td')->addChild('input');
        $app->form->table->tr->td[1]->input['type'] = "text";
        $app->form->table->tr->td[1]->input['name'] = "vin";
        $app->form->table->tr->td[1]->input['maxlength'] = "8";

        $app->form->table->addChild('tr');
        $app->form->table->tr[1]->addChild('td', "Date: (yyyy-mm-dd)");
        $app->form->table->tr[1]->addChild('td')->addChild('input');
        $app->form->table->tr[1]->td[1]->input['type'] = "text";
        $app->form->table->tr[1]->td[1]->input['name'] = "date";
        $app->form->table->tr[1]->td[1]->input['maxlength'] = "10";

        $app->form->table->addChild('tr');
        $app->form->table->tr[2]->addChild('td', "Start Time: (hh:mm)");
        $app->form->table->tr[2]->addChild('td')->addChild('input');
        $app->form->table->tr[2]->td[1]->input['type'] = "text";
        $app->form->table->tr[2]->td[1]->input['name'] = "timeS";
        $app->form->table->tr[2]->td[1]->input['maxlength'] = "5";

        $app->form->table->addChild('tr');
        $app->form->table->tr[3]->addChild('td', "End Time: (hh:mm)");
        $app->form->table->tr[3]->addChild('td')->addChild('input');
        $app->form->table->tr[3]->td[1]->input['type'] = "text";
        $app->form->table->tr[3]->td[1]->input['name'] = "timeE";
        $app->form->table->tr[3]->td[1]->input['maxlength'] = "5";

        $app->form->table->addChild('tr');
        $app->form->table->tr[4]->addChild('td', "Work Performed: ");
        $app->form->table->tr[4]->addChild('td')->addChild('input');
        $app->form->table->tr[4]->td[1]->input['type'] = "text";
        $app->form->table->tr[4]->td[1]->input['name'] = "work";

        $app->form->table->addChild('tr');
        $app->form->table->tr[5]->addChild('td', "Cost: ");
        $app->form->table->tr[5]->addChild('td')->addChild('input');
        $app->form->table->tr[5]->td[1]->input['type'] = "text";
        $app->form->table->tr[5]->td[1]->input['name'] = "cost";


        $app->form->addChild('p')->addChild('input');
        $app->form->p[1]->input['type'] = "submit";
        $app->form->p[1]->input['name'] = "update";
        $app->form->p[1]->input['value'] = "submit";

        $app->form->p[1]->addChild('input');
        $app->form->p[1]->input[1]['type'] = "reset";
        $app->form->p[1]->input[1]['value'] = "reset";
}

echo $tree->asXML();
?>
